﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Car_Rental;
using System.Timers;

namespace Data.Car_Rental
{
    /// <summary>
    /// 分类数据操作
    /// </summary>
    public class CategoryRepository : Car_RentalRepositoryBase, ICategoryRepository
    {
        #region 静态树结构，每1分钟去获一下数据库
        static List<Category> categoryList = null;
        /// <summary>
        /// 数据实体
        /// </summary>
        public static volatile List<Category> Instance = null;
        static CategoryRepository categoryRepository = new CategoryRepository();
        static System.Timers.Timer sysTimer = new System.Timers.Timer(600000);
        static CategoryRepository()
        {
            Reload();//第一次加载
            sysTimer.AutoReset = true;
            sysTimer.Enabled = true;
            sysTimer.Elapsed += new System.Timers.ElapsedEventHandler(sysTimer_Elapsed);
            sysTimer.Start();
        }

        /// <summary>
        /// 被订阅了Elapsed事件的方法,每隔一段时间去重新获取数据列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Reload();
        }

        internal static void Reload()
        {
            categoryList = categoryRepository.GetModel().OrderBy(i => i.SortNumber).ToList();
            Instance = categoryList.Where(i => i.ID != 1).ToList();
        }
        /// <summary>
        /// 分类树结构
        /// </summary>
        /// <returns>项目分类列表</returns>
        public static Category GetTree()
        {
            Category root = new Category();
            try
            {
                root = categoryList.Single(item => item.ID.Equals(Category.RootID));//获取跟节点
                GetSublCategories(root);
            }
            catch (Exception ex)
            {
                throw new Exception("无效根节点" + ex.ToString());
            }
            return root;
        }


        /// <summary>
        /// 根据父对象，找到子孙树
        /// </summary>
        /// <param name="father">父对象</param>
        static void GetSublCategories(Category father)
        {
            father.Sons = categoryList.Where(item =>
                item.ParentID.Equals(father.ID) && item.ID != Category.RootID).ToList();
            father.Sons.ForEach(item =>
            {
                item.Father = father;
                GetSublCategories(item);
            });

        }
        #endregion

        #region IRepository<Category> 成员

        public void Update(Category entity)
        {
            base.UpdateEntity(entity);
        }


        public void Update(IList<Category> list)
        {
            list.ToList().ForEach(entity =>
            {
                this.Update(entity);
            });
        }

        public void Insert(Category entity)
        {
            base.InsertEntity(entity);
        }

        public void Insert(IList<Category> list)
        {
            list.ToList().ForEach(entity =>
            {
                this.Insert(entity);
            });
        }

        public Category InsertGetIDENTITY(Category entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(Category entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<Category> list)
        {
            list.ToList().ForEach(entity =>
            {
                this.Delete(entity);
            });
        }

        public IQueryable<Category> GetModel()
        {
            return base.GetEntities<Category>();
        }

        public Category Find(params object[] keyValues)
        {
            return base.Find<Category>(keyValues);
        }

        #endregion

        #region ICategoryRepository 成员

        public Entity.PagedList<Category> GetCategory(Entity.VPredication vp, Entity.PagingParam pp)
        {
            var linq = this.GetModel();
            return new Entity.PagedList<Category>(linq, pp.PageIndex, pp.PageSize);
        }

        #endregion


    }
}
